﻿angular.module('app.masterdata.mailtemplate', [])
    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider
            .when('/admin/mailtemplate', {
                templateUrl: 'app/masterdata/mailtemplate/mailtemplate-list.tpl.html',
                controller: 'mailtemplateListCtrl'
            });
    } ])


    .controller('mailtemplateListCtrl', ['$scope', '$sce', 'baseDataProxy', 'mailTemplateProxy', 'modalService', function ($scope, $sce, baseDataProxy, mailTemplateProxy, modalService) {

        baseDataProxy.SysTypeDetail("012", function (mts) {
            $scope.mailtypes = mts;
        });
        baseDataProxy.SysTypeDetail("013", function (langs) {
            $scope.maillanguages = langs;
        });

        mailTemplateProxy.all(function (res) {
            $scope.list = res;
        });

        $scope.$on('search', function (e) {
            $scope.search();
        });

        $scope.search = function () {
            var filterStr = '';
            var _English = '001';
            var _Chinese = '002';
            var _First = '001';
            var _Second = '002';
            var _Final = '003';
            if ($scope.language) {
                var language = "";
                switch ($scope.language) {
                    case _English: language = "English";
                        break;
                    case _Chinese: language = "Chinese";
                        break;
                }
                if (filterStr != "") {
                    filterStr += "and (Language eq '" + language + "')";
                } else {
                    filterStr += "&$filter=(Language eq '" + language + "')";
                }
            }

            if ($scope.type) {
                var type = "";
                switch ($scope.type) {
                    case _First: type = "First Reminder";
                        break;
                    case _Second: type = "Second Reminder";
                        break;
                    case _Final: type = "Final Reminder";
                }
                if (filterStr != "") {
                    filterStr += "and (Type eq '" + type + "')";
                } else {
                    filterStr += "&$filter=(Type eq '" + type + "')";
                }
            }

            mailTemplateProxy.odataQuery(filterStr, function (res) {
                $scope.list = res;
            });
        };


        $scope.gridOptions = {
            multiSelect: false,
            enableFullRowSelection: true,
            data: 'list',
            columnDefs: [
                { name: 'subject' },
                { name: 'language' },
                { name: 'type' },
                { name: 'mainBody' },
                { name: 'creater' },
                { name: 'createDate' }
            //                { name: 'ed', displayName: 'Operation', width: '100',
            //                    cellTemplate: '<div class="ngCellText" ng-class="col.colIndex()"><a ng-click="grid.appScope.edit(row.entity)"> Edit </a></div>', pinnedRight: true
            //                }
            ], onRegisterApi: function (gridApi) {

                $scope.gridApi = gridApi;
            }
        };

        $scope.$on('edit', function (e) {
            $scope.edit();
        });

        $scope.edit = function () {
            var row = $scope.gridApi.selection.getSelectedRows()[0];
            var modalDefaults = {
                templateUrl: 'app/masterdata/mailtemplate/mailtemplate-edit.tpl.html',
                controller: 'mailtemplateEditCtrl',
                resolve: {
                    template: function () { return row; },
                    modalOptions: function () {
                        return {
                            headerText: 'Template Edit',
                            closeButtonText: 'Close',
                            actionButtonText: 'Confirm'
                        }
                    },
                    mts: function () { return $scope.mailtypes; },
                    langs: function () { return $scope.maillanguages; }
                },
                windowClass: 'modalService'
            };

            modalService.showModal(modalDefaults, {}).then(function (result) {
                //                    if (result != 'cancel') {
                //                        mailTemplateProxy.all(function (res) {
                //                            $scope.list = res;
                //                        });
                //                    }
                $scope.search();
            });
        };

        $scope.$on('del', function (e) {
            $scope.Del();
        });

        $scope.Del = function () {
            var entity = $scope.gridApi.selection.getSelectedRows()[0];
            entity.$remove(function () {
                $scope.search();
                alert("Delete Success");

            }, function () {
                alert("Delete Error");
            });
        }

        $scope.$on('resetSearch', function (e) {
            $scope.resetSearch();
        });

        $scope.resetSearch = function () {
            filstr = "";
            $scope.language = "";
            $scope.type = "";
        }

        $scope.$on('new', function (e) {
            $scope.New();
        });

        $scope.New = function () {
            var modalDefaults = {
                templateUrl: 'app/masterdata/mailtemplate/mailtemplate-edit.tpl.html',
                controller: 'mailtemplateEditCtrl',
                resolve: {
                    template: function () { return mailTemplateProxy.queryObject(); },
                    modalOptions: function () {
                        return {
                            headerText: 'Template Edit',
                            closeButtonText: 'Close',
                            actionButtonText: 'Confirm'
                        }
                    },
                    mts: function () { return $scope.mailtypes; },
                    langs: function () {return $scope.maillanguages; }
                }
            }

            modalService.showModal(modalDefaults, {}).then(function () {

                $scope.search();
            });
        };

    } ])

    .controller('mailtemplateEditCtrl', ['$scope', '$sce', 'mailTemplateProxy', 'template', 'modalOptions', '$modalInstance', 'mts', 'langs', function ($scope, $sce, mailTemplateProxy, template, modalOptions, $modalInstance, mts, langs) {

       
        $scope.modalOptions = modalOptions;
        $scope.mailtypes = mts;
        $scope.maillanguages = langs;
        $scope.template = template;
        $scope.onSave = function () {
            $modalInstance.close();

        };

        $scope.onUpdate = function () {
            $modalInstance.close();
        };

        $scope.close = function () {
            $modalInstance.close();
        };

    } ]);


